﻿using System;
using System.Collections.Generic;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            IDictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add(key: "tom", value: "tompwd");
            dict.Add(key: "kaka", value: "kakapwd");
            dict.Add(key: "hoho", value: "hohopwd");

            for (; ; )
            {
                Console.WriteLine(value: "请输入用户名:");
                string username = Console.ReadLine();
                if (!dict.ContainsKey(key: username))
                {
                    Console.WriteLine("不存在该用户.");
                    break;
                }

                //
                for (int times = 0; ;)
                {
                    Console.WriteLine(value: "请输入密码:");
                    string password = Console.ReadLine();
                    string pwd = string.Empty;
                    if (!dict.TryGetValue(key: username, out pwd))
                    {
                        Console.WriteLine("查询密码失败.");
                    }
                    if (pwd != password)
                    {
                        times++;
                        if (times >= 3)
                        {
                            Console.WriteLine($"密码已重试失败 {times} 次, 请重新登录.");
                            break;
                        }
                        continue;
                    }
                    else
                    {
                        Console.WriteLine("用户名和密码验证成功, 可以登录");
                        return;
                    }
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}
